package com.eduplatform.controller;

import com.eduplatform.service.AIProxyService;
import com.eduplatform.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * AI服务代理控制器
 * 用于代理处理AI模型API请求，解决前端跨域问题
 */
@RestController
@RequestMapping("/api/ai")
public class AIProxyController {

    @Autowired
    private AIProxyService aiProxyService;

    /**
     * AI服务代理接口
     * 接收前端请求并转发到外部AI服务
     *
     * @param requestBody 包含问题和目标模型的请求体
     * @return AI回答结果
     */
    @PostMapping("/proxy")
    public Result proxyAIRequest(@RequestBody Map<String, String> requestBody) {
        try {
            String question = requestBody.get("question");
            String targetModel = requestBody.get("targetModel");

            if (question == null || question.trim().isEmpty()) {
                return Result.error("问题内容不能为空");
            }

            String aiResponse = aiProxyService.getAIResponse(question, targetModel);

            Map<String, Object> responseData = new HashMap<>();
            responseData.put("content", aiResponse);
            responseData.put("model", targetModel);
            responseData.put("timestamp", System.currentTimeMillis());

            return Result.success(responseData);
        } catch (Exception e) {
            return Result.error("AI服务暂时不可用: " + e.getMessage());
        }
    }
}